package com.acme.mock;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.ClientExistsException;
import com.acme.bankapp.service.bank.BankService;
import com.acme.bankapp.service.email.EmailService;

public class Action {
	private Bank bank;

	public Action(Bank bank) {
		this.bank = bank;
	}

	public enum Command {
		ADD("add (.*)", "addClient"), BYE("bye(.*)", "bye");
		public final String pattern;
		public final String command;

		Command(String pattern, String command) {
			this.pattern = pattern;
			this.command = command;
		}
	}

	public Object ActionHandler(String command, Object... params) {
		switch (command) {
		case "addClient":
			return addClient((String) params[0]);
		case "bye":
			EmailService.getEmailService().closeQueue();
			return "bye";
		}
		return null;
	}

	public Object addClient(String params) {
		Client newClient = BankService.parseClientFromFeed(params);
		try {
			BankService.addClient(bank, newClient);
		} catch (ClientExistsException e) {
			System.err.println(newClient.getClientSalutation() + " added operation failed");
		}
		return newClient;
	}
}
